package cn.zzp.sync;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @ClassName Consumer
 * @Description mq 消费者
 * @Author 祝志鹏
 * @Date 2022/8/16 14:56
 */
public class Consumer {

    public static void main(String[] args) throws MQClientException {

        // 创建对象
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("sync_product_group");

        // 设置nameServer
        consumer.setNamesrvAddr("localhost:9876");

        // 订阅消息
        consumer.subscribe("sync","hello");

        // 监听注册消息
        consumer.registerMessageListener((MessageListenerConcurrently) (list, consumeConcurrentlyContext) -> {
            for (MessageExt messageExt : list) {
                // System.out.println(messageExt);
                System.out.println(new String(messageExt.getBody(),StandardCharsets.UTF_8));
            }

            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });

        // 启动
        consumer.start();

    }
}
